/**
 * This file is part of 1genia trampoline
 * Copyright (C) 2007 1genia (contact@1genia.com)
 *
 * This library is free software; you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; version 3 of the License. 
 *
 * This library is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Library General Public License for more details. 
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; see the file COPYING.TXT.  If not,
 * write to the Free Software Foundation, Inc., 51 Franklin Street,
 * Fifth Floor, Boston, MA 02110-1301, USA. 
 **/
package com.genia.toolbox.form.editor.model.form.impl;

import java.rmi.server.UID;
import java.util.ArrayList;
import java.util.List;

import com.genia.toolbox.web.portlet.description.ContainerPortletDescription.LayoutDirection;

/**
 * The container form model.
 */
public class ContainerFormModel
    extends FormModel
{

  /**
   * The minimum number of sub-forms.
   */
  private Integer minNbSubForms = 0;

  /**
   * The maximum number of sub-forms.
   */
  private Integer maxNbSubForms = Integer.MAX_VALUE;

  /**
   * The layout direction.
   */
  private LayoutDirection direction = null;

  /**
   * The sub-forms.
   */
  private List<FormModel> subforms = null;



  /**
   * Constructor.
   */
  public ContainerFormModel()
  {
    super();
    this.subforms = new ArrayList<FormModel>();
  }



  /**
   * Get the maximum number of sub-forms.
   * 
   * @return the maximum number of sub-forms.
   */
  public Integer getMaxNbSubForms()
  {
    return this.maxNbSubForms;
  }



  /**
   * Set the maximum number of sub-forms.
   * 
   * @param maxNbSubForms
   *          the maximum number of sub-forms.
   */
  public void setMaxNbSubForms(Integer maxNbSubForms)
  {
    this.maxNbSubForms = maxNbSubForms;
  }



  /**
   * Get the minimum number of sub-forms.
   * 
   * @return the minimum number of sub-forms.
   */
  public Integer getMinNbSubForms()
  {
    return this.minNbSubForms;
  }



  /**
   * Set the minimum number of sub-forms.
   * 
   * @param minNbSubForms
   *          the minimum number of sub-forms.
   */
  public void setMinNbSubForms(Integer minNbSubForms)
  {
    this.minNbSubForms = minNbSubForms;
  }



  /**
   * Get the sub-forms.
   * 
   * @return the sub-forms.
   */
  @Override
  public List<FormModel> getSubElements()
  {
    return this.subforms;
  }



  /**
   * Set the sub-forms.
   * 
   * @param subforms
   *          the sub-forms.
   */
  public void setSubforms(List<FormModel> subforms)
  {
    this.subforms = subforms;
  }



  /**
   * Get the direction.
   * 
   * @return the direction.
   */
  public LayoutDirection getDirection()
  {
    return this.direction;
  }



  /**
   * Set the direction.
   * 
   * @param direction
   *          the direction.
   */
  public void setDirection(LayoutDirection direction)
  {
    this.direction = direction;
  }



  /**
   * Update a sub-form.
   * 
   * @param updatedForm
   *          the updated form.
   */
  @Override
  public void updateForm(FormModel updatedForm)
  {
    for (FormModel subform : this.subforms) {
      if (subform.getUniqueID().equals(updatedForm.getUniqueID())) {
        subform = updatedForm;
      }
      else {
        subform.updateForm(updatedForm);
      }
    }
  }



  /**
   * Set the document unique ID.
   * 
   * @param documentID
   *          the document unique ID.
   */
  @Override
  public void setDocumentID(UID documentID)
  {
    super.setDocumentID(documentID);
    for (FormModel submodel : this.getSubElements()) {
      submodel.setDocumentID(documentID);
    }
  }



  /**
   * Get the number of sub-forms.
   * 
   * @return the number of sub-forms.
   */
  public int getNbSubForms()
  {
    int nbDisplayedSubForms = 0;
    if (this.getSubElements() != null && !this.getSubElements().isEmpty()) {
      nbDisplayedSubForms = this.getSubElements().size();
    }
    return nbDisplayedSubForms;
  }



  /**
   * Get whether the element has at least one sub-element.
   * 
   * @return whether the element has at least one sub-element.
   */
  @Override
  public boolean isContainer()
  {
    return true;
  }



  /**
   * Get whether the settings are valid.
   * 
   * @return whether the settings are valid.
   */
  @Override
  public boolean accept()
  {
    boolean isOK = true;
    if (this.getName() == null || this.getName().trim().length() == 0) {
      isOK = false;
    }
    if (FormType.LABELED.compareTo(this.getType()) == 0) {
      if (this.getLabelKey() == null || this.getLabelKey().trim().length() == 0) {
        isOK = false;
      }
    }
    int nbSubForms = 0;
    if (this.getSubElements() != null) {
      nbSubForms = this.getSubElements().size();
    }
    if (nbSubForms < this.getMinNbSubForms()) {
      isOK = false;
    }
    for (FormModel model : this.getSubElements()) {
      if (!model.accept()) {
        isOK = false;
        break;
      }
    }
    return isOK;
  }

}
